package com.app.couponsharelife.ui.main.me.problem

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.CommonProblemBean
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.ui.adapter.ProblemListAdapter
import com.app.couponsharelife.ui.listener.OnClickListener
import com.app.couponsharelife.ui.viewmodel.MeViewModel
import com.app.couponsharelife.utils.AppUtils
import com.app.fuquan.data.bean.PageStatus
import kotlinx.android.synthetic.main.activity_problem.*
import kotlinx.android.synthetic.main.common_tool_bar_layout.*
import java.util.HashMap

class ProblemActivity : BaseActivity() {

    override val viewLayoutId: Int get() = R.layout.activity_problem

    private var mViewModel: MeViewModel? = null

    override fun initViews() {
        AppUtils.initImmersionBar(this,false)
        initToolBar()
        initRecyclerView()
    }

    private fun initToolBar() {
        ivBack.setOnClickListener { finish() }
        tvToolBarTitle.text = "常见问题"
    }

    private fun initRecyclerView() {
        mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        mRecyclerView.adapter = ProblemListAdapter(arrayListOf(PageTipBean("", 0, 0)), object : OnClickListener {
            override fun click(arg1: Int, arg2: Any) {

            }
        })
        (mRecyclerView.itemAnimator as androidx.recyclerview.widget.DefaultItemAnimator).supportsChangeAnimations = false
    }

    override fun initViewModel() {
        if (mViewModel == null) mViewModel = ViewModelProviders.of(this).get(MeViewModel::class.java)
        if (!mViewModel!!.getCommonProblems()!!.hasObservers()) {
            mViewModel!!.getCommonProblems()!!.observe(this, Observer {
                if (it == null) {
                    dismissProgressDialog()
                    handleError(parseErrorDate("数据出错啦"))
                    return@Observer
                }
                when (it.status) {
                    PageStatus.Loading -> {
                    }
                    PageStatus.Error -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetCommonProblems(hashMapOf(), false)
                        handleError(parseErrorDate(it.error!!.message))
                    }
                    PageStatus.Empty -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetCommonProblems(hashMapOf(), false)
                        handleError(parseErrorDate("数据出错啦"))
                    }
                    PageStatus.Content -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetCommonProblems(hashMapOf(), false)
                        if (it.data!!.code == 200 && it.data.data != null) {
                            handleContent(it.data.data!!)
                        } else {
                            handleError(parseErrorDate("获取失败"))
                        }
                    }
                }
            })
        }
        getCommonProblems()
    }

    private fun handleContent(data: List<CommonProblemBean>) {
        if (data.isNotEmpty()) {
            (mRecyclerView.adapter as ProblemListAdapter).setData(data)
        } else {
            handleError(parseErrorDate("暂无数据"))
        }
    }

    private fun handleError(pageTipBean: PageTipBean) {
        if ((mRecyclerView.adapter as ProblemListAdapter).list[0] is PageTipBean) {
            ((mRecyclerView.adapter as ProblemListAdapter).list[0] as PageTipBean).tip = pageTipBean.tip
            ((mRecyclerView.adapter as ProblemListAdapter).list[0] as PageTipBean).status = pageTipBean.status
            ((mRecyclerView.adapter as ProblemListAdapter).list[0] as PageTipBean).src = pageTipBean.src
            (mRecyclerView.adapter as ProblemListAdapter).notifyItemChanged(0)
        }
    }

    private fun getCommonProblems() {
        val map = HashMap<String, String>()
        mViewModel?.freshGetCommonProblems(map, true)
    }

    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}
